package handler

import (
	"net/http"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

const (
	updateClassificationSuccess = "更新分类成功"
)

type changeCommissionedTransactionParams struct {
	ID             int64   `json:"employee_id" binding:"required"`
	CommissionRate float64 `json:"commission_rate" binding:"required"`
	Salary         float64 `json:"salary" binding:"required"`
}

func (h *Handler) ChangeCommissionedTransaction(ctx *gin.Context) {
	var req changeCommissionedTransactionParams
	// Bind incoming json to struct and check for validation errors
	if ok := bindData(ctx, &req); !ok {
		return
	}

	c := &model.CommissionedClassification{
		CommissionRate: req.CommissionRate,
		Salary:         req.Salary,
	}

	if err := h.CommissionedClassificationService.Update(ctx, req.ID, c); err != nil {
		zap.L().Error("service failed to update commissioned transaction", zap.Error(err))
		ctx.JSON(apperrors.Status(err), gin.H{
			"error": err,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg": updateClassificationSuccess,
	})
}
